<?php

namespace App\Home\Controllers;

use App\Home\Filters\UserFilter;
use App\Home\Models\User;
use App\Home\Requests\UserRequest;
use App\Home\Resources\UserResource;
use Illuminate\Http\Request;

class UserController extends Controller
{
    public function store(UserRequest $request)
    {
        $inputs = $request->validated();
        $user = User::create($inputs);

        return $this->created(UserResource::make($user));
    }

    public function update(UserRequest $request, User $user)
    {
        $inputs = $request->validated();
        $user->update($inputs);

        return $this->created(UserResource::make($user));
    }

    public function destroy(User $user)
    {
        $user->delete();
        return $this->noContent();
    }

    public function index(UserFilter $filter)
    {
        $users = User::query()
            ->filter($filter)
            ->paginate();

        return $this->ok(UserResource::collection($users));
    }

    public function show(Request $request, User $user)
    {
        return $this->ok(UserResource::make($user));
    }
}
